import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';

void getHttp() async {
  try {
    var response = await Dio().get('http://www.baidu.com');
    if (kDebugMode) {
      print(response);
    }
  } catch (e) {
    if (kDebugMode) {
      print(e);
    }
  }
}

class ScaffoldExample extends StatefulWidget {
  const ScaffoldExample({Key? key}) : super(key: key);

  @override
  State<ScaffoldExample> createState() => _ScaffoldExampleState();
}

class _ScaffoldExampleState extends State<ScaffoldExample>
    with SingleTickerProviderStateMixin {
  bool _isOpen = false;
  late AnimationController _animationController;
  late Animation<Color?> _animationColor;
  late Animation<double> _animationIcon;
  final Curve _curve = Curves.easeOut;

  final GlobalKey<ScaffoldState> _scaffoldState = GlobalKey();
  final GlobalKey _appBarKey = GlobalKey();

  int _currentIndex = 0;

  final List<Widget> _pages = [
    Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Center(
          child: Text('ScaffoldExample'),
        ),
        buildMaterialButton(),
      ],
    ),
    Center(
      child: Text('tab2'),
    ),
  ];

  @override
  void initState() {
    super.initState();

    _animationController = AnimationController(
      vsync: this,
      duration: Duration(milliseconds: 600),
    )..addListener(
        () {
          setState(() {
            //
          });
        },
      );

    _animationColor = ColorTween(
      begin: Colors.red,
      end: Colors.green,
    ).animate(
      CurvedAnimation(
        parent: _animationController,
        curve: Interval(0.0, 1.0, curve: _curve),
      ),
    );

    _animationIcon = Tween(begin: 0.0, end: 1.0).animate(_animationController);
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldState,
      appBar: AppBar(
        key: _appBarKey,
        centerTitle: true,
        title: Text('ScaffoldExample'),
        leading: IconButton(
          onPressed: () {
            _scaffoldState.currentState?.openDrawer();
          },
          icon: Icon(Icons.menu_open),
        ),
        actions: [
          IconButton(
            onPressed: () {
              _scaffoldState.currentState?.openEndDrawer();
            },
            icon: Icon(Icons.menu_rounded),
          ),
        ],
      ),
      body: _pages[_currentIndex],
      floatingActionButton: FloatingActionButton(
        backgroundColor: _animationColor.value,
        onPressed: () {
          if (!_isOpen) {
            _animationController.forward();
          } else {
            _animationController.reverse();
          }
          _isOpen = !_isOpen;
        },
        child: AnimatedIcon(
          icon: AnimatedIcons.menu_close,
          progress: _animationIcon,
          // color: Colors.white,
        ),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
      floatingActionButtonAnimator: FloatingActionButtonAnimator.scaling,
      persistentFooterButtons: [
        // 用于隐私政策
        TextButton(onPressed: () {}, child: Text('取消')),
        TextButton(onPressed: () {}, child: Text('确认授权')),
      ],
      drawer: Drawer(
        elevation: 20.0,
        child: Center(
          child: Text('drawer'),
        ),
      ),
      onDrawerChanged: (isOpen) {
        if (kDebugMode) {
          print('onDrawerChanged $isOpen');
        }
        setState(() {});
      },
      endDrawer: Drawer(
        elevation: 20.0,
        child: Center(
          child: Text('drawer'),
        ),
      ),
      onEndDrawerChanged: (isOpen) {
        if (kDebugMode) {
          print('onEndDrawerChanged $isOpen');
        }
        setState(() {});
      },
      // 拖动行为方式，与 drawer 配合使用，用于打开和关闭抽屉的拖动行为将在检测到拖动手势时开始。 如果设置为 DragStartBehavior.down，它将在首次检测到 down 事件时开始。当拖动返回时会使用 DragStartBehavior.down 是有很明显的卡顿，建议使用 DragStartBehavior.start
      drawerDragStartBehavior: DragStartBehavior.start,
      drawerScrimColor: Colors.blue,
      drawerEdgeDragWidth: 200, // 控制左侧滑栏拉出多少阈值算打开
      drawerEnableOpenDragGesture: true, // 是否开启左侧边栏手势拖动
      endDrawerEnableOpenDragGesture: false, // 是否开右侧边栏启手势拖动
      bottomNavigationBar: BottomNavigationBar(
        items: [
          BottomNavigationBarItem(
            icon: Icon(Icons.ac_unit),
            label: '首页',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.my_library_add),
            label: '我的',
          ),
        ],
        currentIndex: _currentIndex,
        onTap: (currentIndex) {
          setState(() {
            _currentIndex = currentIndex;
          });
        },
      ),
      // 持久在body下方，底部控件上方的控件
      bottomSheet: Row(children: [
        Expanded(child: TextField()),
        TextButton(onPressed: () {}, child: Text('发送')),
      ]),
      backgroundColor: Colors.orange, // scaffold 背景色
      // resizeToAvoidBottomInset: false, // 设置为 false 键盘弹起会遮挡 bottomSheet 组件， 反之不遮挡
      // primary: true, // 脚手架是否延伸到顶部，一般不去动
      extendBody: false, // 是否延伸到底部，主要用于做半透明效果
      extendBodyBehindAppBar: false, // 是否延伸到顶部，用于做半透明、毛玻璃效果的主要控制属性
      restorationId: 'scaffold',
    );
  }
}

MaterialButton buildMaterialButton() {
  return MaterialButton(
    //背景颜色
    color: Colors.white,
    //边框样式
    shape: const RoundedRectangleBorder(
      //边框颜色
      side: BorderSide(
        color: Colors.deepPurple,
        width: 1,
      ),
      //边框圆角
      borderRadius: BorderRadius.all(
        Radius.circular(8),
      ),
    ),
    //按钮高度
    height: 20,
    //按钮最小宽度
    minWidth: 40,
    //点击事件
    onPressed: () async {
      getHttp();
    },
    child: const Text(
      '查找',
      style: TextStyle(
        fontSize: 12,
        color: Colors.deepPurple,
      ),
    ),
  );
}
